from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Text
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship

from app.database import Base


class PredictionLog(Base):
    """预测日志模型
    
    用于存储模型预测结果和相关指标
    """
    __tablename__ = "prediction_logs"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
    model_type = Column(String(50), nullable=False)  # CNN, LSTM, CNN+LSTM
    dataset_name = Column(String(255), nullable=False)
    dataset_path = Column(String(255), nullable=False)
    accuracy = Column(Float, nullable=False)
    precision_score = Column(Float, nullable=False)
    recall_score = Column(Float, nullable=False)
    f1_score = Column(Float, nullable=False)
    loss = Column(Float, nullable=True)
    sample_count = Column(Integer, nullable=False)
    feature_count = Column(Integer, nullable=False)
    class_count = Column(Integer, nullable=False)
    class_distribution = Column(Text, nullable=True)  # JSON格式存储
    prediction_time = Column(Float, nullable=False)
    result_file_path = Column(String(255), nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关联
    user = relationship("User", back_populates="prediction_logs") 